/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;
import model.Comment;
import model.CommentQuery;
import model.Event;
import model.EventQuery;
import model.HistoryPayment;
import model.HistoryPaymentQuery;

/**
 *
 * @author Agus
 */
public class PaymentLogic {

    HttpServletRequest request;

    public PaymentLogic(HttpServletRequest request) {
        this.request = request;
    }
    /* method untuk melakukan fungsi tambahHistoryPayment, yaitu mengambil nilai textfield pada form payment
     * untuk kemudian dimasukkan sebagai nilai untuk membuat baris baru pada tabel payments
     */

    public String tambahHistoryPayment() {
        Event event = new Event();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        List<HistoryPayment> historyPayments = new ArrayList<HistoryPayment>();
        HistoryPayment historyPayment = new HistoryPayment();
        HistoryPaymentQuery hpq = new HistoryPaymentQuery();
        HttpSession session = request.getSession();
        List<Comment> comments = new ArrayList<Comment>();
        CommentQuery cq = new CommentQuery();

        String namaPayer = request.getParameter("namaPayer");
        String emailPayer = request.getParameter("emailPayer");
        String temp = request.getParameter("jumlahTiket");
        String temp2 = request.getParameter("totalPayment");
        String tanggalPayment = request.getParameter("tanggalPayment");
        Event ename = (Event) session.getAttribute("ename");
        if (namaPayer != null) {
            historyPayment.setIdEvent(ename);
            historyPayment.setNamaPayer(namaPayer);
            historyPayment.setEmailPayer(emailPayer);
            historyPayment.setTanggalPayment(tanggalPayment);
            session.setAttribute("tempAddHistoryPayment", historyPayment);
        } else {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahPayment.jsp";
        }
        if (temp.equals("") || temp2.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahPayment.jsp";
        } else {
            try {
                int jumlahTiket = Integer.parseInt(temp);
                historyPayment.setJumlahTiket(jumlahTiket);
            } catch (NumberFormatException nf) {
                session.setAttribute("error", "format jumlah tiket harus angka");
                return "tambahPayment.jsp";
            }
            try {
                int totalPayment = Integer.parseInt(temp2);
                historyPayment.setTotalPayment(totalPayment);
            } catch (NumberFormatException nfe) {
                session.setAttribute("error", "format total pembayaran harus angka");
                return "tambahPayment.jsp";
            }
        }
        session.setAttribute("tempAddHistoryPayment", historyPayment);
        if (namaPayer.equals("") || emailPayer.equals("")
                || tanggalPayment.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahPayment.jsp";
        } else {
            try {
                DateFormat formatter;
                Date date;
                formatter = new SimpleDateFormat("dd-MM-yyyy");
                date = (Date) formatter.parse(tanggalPayment);
            } catch (ParseException e) {
                session.setAttribute("error", "tanggal salah, gunakan datepicker");
                return "tambahPayment.jsp";
            }
            hpq.createHistoryPayment(historyPayment);
            session.removeAttribute("tempAddHistoryPayment");
            comments = cq.getComments(ename);
            request.setAttribute("comments", comments);
            return "lihatEventPublic.jsp";
        }
    }
    /* method untuk menghapus payment berdasarkan id dari payment bersangkutan
     */

    public String hapusHistoryPayment() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        String idPayment = request.getParameter("idPayment");
        List<HistoryPayment> historyPayments = new ArrayList<HistoryPayment>();
        HistoryPaymentQuery hpq = new HistoryPaymentQuery();
        List<Event> events = new ArrayList<Event>();
        EventQuery eq = new EventQuery();
        if (session.getAttribute("uname") != null) {
            if (idPayment == null) {
                Event event = (Event) session.getAttribute("ename");
                historyPayments = hpq.getHistoryPayments(event);
                request.setAttribute("historyPayments", historyPayments);
            } else {
                int id = Integer.parseInt(idPayment);
                hpq.deleteHistoryPayment(id);
                Event event = (Event) session.getAttribute("ename");
                historyPayments = hpq.getHistoryPayments(event);
                request.setAttribute("historyPayments", historyPayments);
            }
            return "lihatPayment.jsp";
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            events = eq.getLimitedEvents();
            request.setAttribute("events", events);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Events</a>");
            return "index.jsp";
        }
    }
}
